package com.jastar.android.game.cannon.model;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

public class Cannon {
    private final static float RADIUS = 30F;

    private float x, y;
    private float angle;
    private float power;
    private float barrelLength;
    private Paint paint, powerPaint;

    public Cannon(float x, float y) {
        this.x = x;
        this.y = y;
        this.angle = 45; // 默认45度角
        this.barrelLength = RADIUS * 3; // 炮管长度

        paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(10);
        paint.setAntiAlias(true);

        powerPaint = new Paint();
        powerPaint.setColor(Color.GREEN);
        powerPaint.setStrokeWidth(6);
    }

    public void setAngle(float angle) {
        this.angle = Math.max(0, Math.min(90, angle)); // 限制在0-90度
    }

    public void setPower(float power) {
        this.power = Math.max(10, Math.min(100, power)); // 限制在10-100
    }

    public float getAngle() {
        return angle;
    }

    public float getPower() {
        return power;
    }

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }

    public float getBarrelLength() {
        return barrelLength;
    }

    public void draw(Canvas canvas) {

        // 绘制炮台底座
        RectF oval = new RectF(x - RADIUS, y - RADIUS, x + RADIUS, y + RADIUS);
        canvas.drawArc(oval, -180, 180, false, paint);
        canvas.drawRoundRect(x - RADIUS * 2, y, x + RADIUS * 2, y + RADIUS, RADIUS * 0.5F, RADIUS * 0.4F, paint);

        // 绘制炮管
        float endX = x + barrelLength * (float) Math.cos(Math.toRadians(angle));
        float endY = y - barrelLength * (float) Math.sin(Math.toRadians(angle));
        canvas.drawLine(x, y, endX, endY, paint);

        if (power > 0) {
            float sX = x - RADIUS, sY = y - RADIUS * 2;
            canvas.drawLine(sX, sY, sX + power / 100 * RADIUS * 2, sY, powerPaint);
        }
    }

    public void update() {
        // 炮台本身不需要每帧更新状态
        // 但保留这个方法以保持接口一致性
        // 可以在这里添加炮台动画效果或特殊状态更新
    }

    public void setPosition(float x, float y) {
        this.x = x;
        this.y = y;
    }

}